package servlet.util;

import javax.servlet.http.HttpServletRequest;

public class RequestIpUtil {
	public static String getUserIp(HttpServletRequest request){
		final String ERROR_IP = "127.0.0.1";
		String ip = request.getHeader("X-Real-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("x-forwarded-for");
		}
 
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
			if ("0:0:0:0:0:0:0:1".equals(ip))
			{
				ip = ERROR_IP;
			}
		}
		if ("unknown".equalsIgnoreCase(ip))
		{
			ip = ERROR_IP;
			return ip;
		}
		int index = ip.indexOf(',');
		if (index >= 0)
		{
			ip = ip.substring(0, index);
		}
 
		return ip;

	}
}
